package com.king.scheduler;

import java.util.Date;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzTest implements Job {

	//该方法实现需要执行的任务
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println("Generating report - "
				+ arg0.getJobDetail().getJobDataMap().get("name") + ", group ="
				+ arg0.getJobDetail().getJobDataMap().get("group") + ", type ="
				+ arg0.getJobDetail().getJobDataMap().get("type"));
		System.out.println(new Date().toString());
	}

	public static void main(String[] args) throws SchedulerException {
		// 创建一个Scheduler
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();

		// 创建一个JobDetail，指明name，groupName，以及具体的Job类名，
		// 该Job负责定义需要执行任务
		JobDetail jobDetail = JobBuilder.newJob(QuartzTest.class).withIdentity("Quartz Job1", "Quartz Group1").build();
		jobDetail.getJobDataMap().put("type", "Quartz");

		// 创建一个每周触发的Trigger，指明星期几几点几分执行
		Trigger trigger = TriggerBuilder.newTrigger().withIdentity("Quartz Trigger1", "Quartz Group1").startNow().build();

		scheduler.scheduleJob(jobDetail, trigger);
		scheduler.start();
	}
}